package cn.brilliantfuture.commentry.util;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2012-4-10
 * 
 * Description:
 * 
 */
public class CommEntryUtil {
	
	public static ConcurrentHashMap<String, StringBuffer[]> advertCacheMap = new ConcurrentHashMap<String, StringBuffer[]>();

	public static String encodePassword(String password, String algorithm) {
		byte[] unencodedPassword = password.getBytes();

		MessageDigest md = null;

		try {
			// first create an instance, given the provider
			md = MessageDigest.getInstance(algorithm);
		} catch (Exception e) {
			e.printStackTrace();
			return password;
		}

		md.reset();

		// call the update method one or more times
		// (useful when you don't know the size of your data, eg. stream)
		md.update(unencodedPassword);

		// now calculate the hash
		byte[] encodedPassword = md.digest();

		StringBuffer buf = new StringBuffer();

		for (int i = 0; i < encodedPassword.length; i++) {
			if ((encodedPassword[i] & 0xff) < 0x10) {
				buf.append("0");
			}

			buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
		}

		return buf.toString();
	}
	
	/**
	 * 替换字符位数字
	 * 
	 * @param checkBit
	 *            被替换的字符串
	 * @return String
	 */
	public static String transferCheckBit(String checkBit) {
		String temp = "";
		char[] charArray = checkBit.toCharArray();
		for (char c : charArray)
			switch (c) {
			case 'A':
				temp += 1;
				break;
			case 'B':
				temp += 2;
				break;
			case 'C':
				temp += 3;
				break;
			case 'D':
				temp += 4;
				break;
			case 'E':
				temp += 5;
				break;
			case 'F':
				temp += 6;
				break;
			default:
				temp += c;
				break;
			}
		return temp;
	}

	/**
	 * 获取当前日期是周几
	 * 
	 * @return int
	 */
	public static int dayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 混淆计算
	 * 
	 * @param str
	 *            被混淆数据
	 * @return String
	 */
	public static String confuse(String str) {
		StringBuffer sb = new StringBuffer();
		for (char c : str.toCharArray())
			switch (c) {
			case '1':
				sb.append(7);
				break;
			case '2':
				sb.append(4);
				break;
			case '3':
				sb.append(6);
				break;
			case '4':
				sb.append(8);
				break;
			case '5':
				sb.append(9);
				break;
			case '6':
				sb.append(5);
				break;
			case '7':
				sb.append(2);
				break;
			case '8':
				sb.append(0);
				break;
			case '9':
				sb.append(1);
				break;
			case '0':
				sb.append(3);
				break;
			}

		return sb.toString();
	}

	/**
	 * Hash计算
	 * 
	 * @param source
	 *            源数据
	 * @return String
	 */
	public static String bernstein(String source) {
		BigDecimal hash = new BigDecimal(0);
		BigDecimal constant = new BigDecimal(333);

		for (int i = 0; i < source.length(); i++)
			hash = constant.multiply(hash).add(new BigDecimal(source.charAt(i)));
		return hash.toString();
	}

	/**
	 * 按照指定长度组织字符串
	 * 
	 * @param source
	 *            原数据
	 * @param length
	 *            数据长度
	 * @return String
	 */
	public static String getFormatStr(String source, int length) {
		int temp = length - source.length();
		for (int i = 0; i < temp; i++) {
			source = "0" + source;
		}
		return source;
	}
	
	public static String randomPasswd() {
		String passwd = String.valueOf((long) (Math.random() * 1000000));

		while (passwd.length() < 6) {
			passwd += String.valueOf((long) (Math.random() * 10));
		}

		return passwd;
	}
}
